
refr_period = 100;
start_fr_l = 1;
last_fr_l = 72000; %%% light: 1:72000; dark 72001:82000
start_fr_d = 72001;
last_fr_d = 82000; %%% light: 1:72000; dark 72001:82000
% % % % act_dF_smooth=zeros(length(selROIs1),length(ROIs(1).activity(start_fr:last_fr)));
% % % % act_dF=zeros(length(selROIs1),length(ROIs(1).activity(start_fr:last_fr)));
% % % % act_ind=zeros(length(selROIs1),length(ROIs(1).activity(start_fr:last_fr)));
% % % % act_freq=zeros(length(selROIs1),length(ROIs(1).activity(start_fr:last_fr)));
% % % % 

selROIs;

for 
temp = regexprep([selROIs.animal_type],'WT','1 ');
temp = regexprep(temp,'APP','0 ');
temp = str2num(temp);
animaltype = logical(temp);

acttemp=[selROIs(~animaltype).activity];
acttemp=acttemp';


for ind=1:size(acttemp,1)
    ind
   %ROIs(ind).activity=ROIs(ind).activity - median(ROIs(ind).activity);
   %act_dF(ind,:) = selROIs1(ind).activity(start_fr_l:last_fr_l)/median(selROIs1(ind).activity(start_fr_l:last_fr_l));
   act_dF(ind,:)=acttemp(ind,start_fr_l:last_fr_l)/median(acttemp(ind,start_fr_l:last_fr_l));
  %act_dF_smooth(ind,:) = psmooth(act_dF(ind,:));
   act_ind(ind,:) = act_dF(ind,:)>(mean(act_dF(ind,:))+1.5*std(act_dF(ind,:)));
   
   act_freq_all(ind,:) = diff(act_ind(ind,:))==1;
   
   
  
   for ond=2:size(act_dF,2)
    
       if ond <= refr_period
           act_freq(ind,ond-1) = act_freq_all(ind,ond-1)==1 && (sum(act_ind(ind,(ond:ond+50)))>3) && sum(act_ind(ind,1:ond-1))<2;
                     
       else
           if ond+50<size(act_dF,2)
              act_freq(ind,ond-1) = act_freq_all(ind,ond-1)==1 && (sum(act_ind(ind,(ond:ond+50)))>3) && sum(act_ind(ind,(ond-refr_period):ond-1))<2;
           else
              act_freq(ind,ond-1) = act_freq_all(ind,ond-1)==1 && sum(act_ind(ind,(ond-refr_period):ond-1))<2;
           end
       end
   end
   
  neuronal_act(ind)=sum(act_freq(ind,:));
   
   
end


ind=[];
ond=[];

for ind=1:size(acttemp,1)
   ind 
   %ROIs(ind).activity=ROIs(ind).activity - median(ROIs(ind).activity);
   %act_dF_d(ind,:) = selROIs1(ind).activity(start_fr_d:last_fr_d)/median(selROIs1(ind).activity(start_fr_d:last_fr_d));
   act_dF_d(ind,:)=acttemp(ind,start_fr_d:last_fr_d)/median(acttemp(ind,start_fr_d:last_fr_d));
   %act_dF_smooth(ind,:) = psmooth(act_dF(ind,:));
   act_ind_d(ind,:) = act_dF_d(ind,:)>(mean(act_dF_d(ind,:))+1.5*std(act_dF_d(ind,:)));
   
   act_freq_all_d(ind,:) = diff(act_ind_d(ind,:))==1;
   
   
  
   for ond=2:size(act_dF_d,2)
 
       if ond <= refr_period
           act_freq_d(ind,ond-1) = act_freq_all_d(ind,ond-1)==1 && (sum(act_ind_d(ind,(ond:ond+50)))>3) && sum(act_ind_d(ind,1:ond-1))<2;
                     
       else
           if ond+50<size(act_dF_d,2)
              act_freq_d(ind,ond-1) = act_freq_all_d(ind,ond-1)==1 && (sum(act_ind_d(ind,(ond:ond+50)))>3) && sum(act_ind_d(ind,(ond-refr_period):ond-1))<2;
           else
              act_freq_d(ind,ond-1) = act_freq_all_d(ind,ond-1)==1 && sum(act_ind_d(ind,(ond-refr_period):ond-1))<2;
           end
       end
   end
   
   neuronal_act_d(ind)=sum(act_freq_d(ind,:));
   
  
   
   
end


for ind=1:size(acttemp,1)
    
act_count_roi(ind)=sum(act_ind(ind,:))
end

time_active=mean(act_count_roi)


for ind=1:size(acttemp,1)
    
act_count_roi_d(ind)=sum(act_ind_d(ind,:))
end

time_active_d=mean(act_count_roi_d)





%%% ROIs selection

%SL_120207_b spot 3 WT
selection=[2 6 9 10 18 34 43 46 57 60 76];
for und=1:length(selection)
selROIs1(und).activity=ROIs(selection(und)).activity;
end

%SL_120207_a spot 3 WT
selection=[2 4 5 8 10 17 19 21 22 23 24 30 37 38 39 47];
for und=1:length(selection)
selROIs2(und).activity=ROIs(selection(und)).activity;
end

%SL_120207_a spot 4 WT
selection=[4 6 8 16 17 21 22 24 25 30 37 38 40 43 45 52];
for und=1:length(selection)
selROIs3(und).activity=ROIs(selection(und)).activity;
end

%SL_120202_b spot 3 APP
selection=[1 7 9 11 13 22 23 24 25 26 30 32 33 40 44 45 60];
for und=1:length(selection)
selROIs4(und).activity=ROIs(selection(und)).activity;
end

%SL_120202_b spot 2 - only FB-PB APP
selection=[4 5 7 9 15 16 18 19 20 22 26 29 30 31 32 33 34 37 44 46 48 56];
for und=1:length(selection)
selROIs5(und).activity=ROIs(selection(und)).activity;
end

%SL_120202_c spot 1 APP
selection=[1 2 3 5 6 7 9 10 13 15 19 20 21 24 26 28 29 30 31 33 34 35 38];
for und=1:length(selection)
selROIs6(und).activity=ROIs(selection(und)).activity;
end

%SL_120202_c spot 2 APP
selection=[2 4 5 7 11 12 15 17 19 21 24 25];
for und=1:length(selection)
selROIs7(und).activity=ROIs(selection(und)).activity;
end




figure; hist(neuronal_act);

figure; plot(act_dF');

thresh(ind)=(mean(act_dF(ind,:))+1.5*std(act_dF(ind,:)))
figure; plot(act_ind(ind,:)+1,'.');
hold on
plot(act_dF(ind,:), 'r');
hold on
plot([1 length(act_dF(ind,:))],[1 1]*thresh(ind),'k');
plot(act_freq(ind,:)+1,'g');


figure;imagesc(act_dF)
